package topin.facecat.FaceCat.Product.Script

import android.widget.Toast
import facecat.topin.chart.*
import facecat.topin.core.FCTran
import facecat.topin.core.FCView
import facecat.topin.core.RefObject
import facecat.topin.grid.FCGrid
import facecat.topin.xml.FCUIXml
import topin.facecat.FaceCat.MainActivity


class NFunctionEx(/*
    * 脚本
    */
    var m_indicator: FCScript, id: Int, name: String, xml: FCUIXml?
) :
    CFunction() {

    /*
    * XML对象
    */
    var m_xml: FCUIXml?

    /*
    * 计算
    */
    override fun onCalculate(`var`: CVariable): Double {
        return when (`var`.m_functionID) {
            STARTINDEX + 0 -> GETATTRIBUTE(`var`)
            STARTINDEX + 1 -> SETATTRIBUTE(`var`)
            STARTINDEX + 2 -> GETSENDER(`var`)
            STARTINDEX + 3 -> ALERT(`var`)
            STARTINDEX + 4 -> INVALIDATE(`var`)
            STARTINDEX + 5 -> STARTTIMER(`var`)
            STARTINDEX + 6 -> STOPTIMER(`var`)
            STARTINDEX + 7 -> GETTOUCHBUTTON(`var`).toDouble()
            STARTINDEX + 8 -> GETTOUCHPOINT(`var`).toDouble()
            STARTINDEX + 9 -> GETCLICKS(`var`).toDouble()
            STARTINDEX + 10 -> GETKEY(`var`).toDouble()
            STARTINDEX + 11 -> UPDATE(`var`)
            STARTINDEX + 12 -> CALLFUNCTION(`var`)
            STARTINDEX + 13 -> GETNEWTIMERID(`var`)
            else -> 0.0
        }
    }

    /*
    * 获取最新的秒表ID
    */
    fun GETNEWTIMERID(`var`: CVariable?): Double {
        return FCView.getNewTimerID().toDouble()
    }

    fun CALLFUNCTION(`var`: CVariable): Double {
        val name = m_indicator.getText(`var`.m_parameters[0])
        val view = m_xml!!.findView(name)
        val propertyName = m_indicator.getText(`var`.m_parameters[1])
        val propertyValue = m_indicator.getText(`var`.m_parameters[2])
        val result = view.callFunction(propertyName, propertyValue)
        val text = "'$result'"
        m_indicator.m_resultVar = CVar()
        m_indicator.m_resultVar.m_type = 1
        m_indicator.m_resultVar.m_str = text
        return text.length.toDouble()
    }

    /*
    * 弹出提示
    */
    private fun ALERT(`var`: CVariable): Double {
        Toast.makeText(
            MainActivity.m_mainActivity,
            m_indicator.getText(`var`.m_parameters[0]),
            Toast.LENGTH_SHORT
        ).show()
        return 0.0
    }

    /*
    * 获取点击次数
    */
    fun GETCLICKS(`var`: CVariable?): Int {
        return m_xml!!.event.touchInfo.m_clicks
    }

    /*
    * 获取按键
    */
    fun GETKEY(`var`: CVariable?): Int {
        return m_xml!!.event.key.toInt()
    }

    /*
    * 获取鼠标按键
    */
    fun GETTOUCHBUTTON(`var`: CVariable?): Int {
        return if (m_xml!!.event.touchInfo.m_firstTouch) {
            1
        } else if (m_xml!!.event.touchInfo.m_secondTouch) {
            2
        } else {
            0
        }
    }

    /*
    * 获取鼠标按键
    */
    fun GETTOUCHPOINT(`var`: CVariable): Int {
        val touchPoint = m_xml!!.event.touchInfo.m_firstPoint
        val newVar = CVariable(m_indicator)
        newVar.m_expression = FCTran.intToStr(touchPoint.x)
        m_indicator.setVariable(`var`.m_parameters[0], newVar)
        val newVar2 = CVariable(m_indicator)
        newVar2.m_expression = FCTran.intToStr(touchPoint.y)
        m_indicator.setVariable(`var`.m_parameters[1], newVar2)
        return 1
    }

    /*
    * 获取属性
    */
    fun GETATTRIBUTE(`var`: CVariable): Double {
        val fScript = m_xml!!.script as FaceCatScript
        if (`var`.m_parameters.size == 2) {
            val name = m_indicator.getText(`var`.m_parameters[0])
            val propertyName = m_indicator.getText(`var`.m_parameters[1])
            var text: String? = ""
            if (name.length == 0) {
                if (m_xml!!.native.focusedView != null) {
                    text = m_xml!!.native.focusedView.getAttribute(propertyName)
                }
            } else {
                text = fScript.getAttribute(name, propertyName)
            }
            m_indicator.m_resultVar = CVar()
            m_indicator.m_resultVar.m_type = 1
            m_indicator.m_resultVar.m_str = "'$text'"
        } else {
            val name = m_indicator.getText(`var`.m_parameters[0])
            var view = m_xml!!.findView(name)
            if (name.length == 0) {
                view = m_xml!!.native.focusedView
            }
            var text = ""
            if (view != null) {
                val grid = view as? FCGrid
                if (grid != null) {
                    val propertyName = m_indicator.getText(`var`.m_parameters[1])
                    if (propertyName.indexOf("cell.") != -1) {
                        var rowIndex = -1
                        if (m_indicator.getText(`var`.m_parameters[2]) == "selectedrow") {
                            if (grid.selectedRows.size > 0) {
                                rowIndex = grid.selectedRows[0].index
                            }
                        } else {
                            rowIndex = m_indicator.getValue(`var`.m_parameters[2]).toInt()
                        }
                        if (rowIndex != -1) {
                            val colName =
                                m_indicator.getText(`var`.m_parameters[3])
                            val row = grid.getRow(rowIndex)
                            if (row != null) {
                                val cell = row.getCell(colName)
                                val argType = ""
                                val refText =
                                    RefObject(text)
                                val refType = RefObject("")
                                cell.getAttribute(
                                    propertyName.replace("cell.", ""),
                                    refText,
                                    refType
                                )
                                text = refText.argvalue
                            }
                        }
                    }
                }
            }
            m_indicator.m_resultVar = CVar()
            m_indicator.m_resultVar.m_type = 1
            m_indicator.m_resultVar.m_str = "'$text'"
        }
        return 0.0
    }

    /*
    * 获取调用者
    */
    fun GETSENDER(`var`: CVariable): Double {
        val fScript = m_xml!!.script as FaceCatScript
        val text = fScript.sender
        if (`var`.m_parameters == null || `var`.m_parameters.size == 0) {
            m_indicator.m_resultVar = CVar()
            m_indicator.m_resultVar.m_type = 1
            m_indicator.m_resultVar.m_str = "'$text'"
        } else {
            val newVar = CVariable(m_indicator)
            newVar.m_expression = "'$text'"
            m_indicator.setVariable(`var`.m_parameters[0], newVar)
        }
        return 0.0
    }

    /*
    * 刷新界面
    */
    private fun INVALIDATE(`var`: CVariable): Double {
        if (m_xml != null) {
            val pLen = if (`var`.m_parameters != null) `var`.m_parameters.size else 0
            if (pLen == 0) {
                m_xml!!.native.invalidate()
            } else {
                val control =
                    m_xml!!.findView(m_indicator.getText(`var`.m_parameters[0]))
                control?.invalidate()
            }
        }
        return 0.0
    }

    /*
    * 设置属性
    */
    private fun SETATTRIBUTE(`var`: CVariable): Double {
        val script = m_xml!!.script as FaceCatScript
        val name = m_indicator.getText(`var`.m_parameters[0])
        var view = m_xml!!.findView(name)
        if (`var`.m_parameters.size == 3) {
            val propertyName = m_indicator.getText(`var`.m_parameters[1])
            val propertyValue = m_indicator.getText(`var`.m_parameters[2])
            if (name.length == 0) {
                if (m_xml!!.native.focusedView != null) {
                    m_xml!!.native.focusedView.setAttribute(propertyName, propertyValue)
                }
            } else {
                script.setAttribute(name, propertyName, propertyValue)
            }
        } else {
            if (name.length == 0) {
                view = m_xml!!.native.focusedView
            }
            if (view != null) {
                val grid = view as? FCGrid
                if (grid != null) {
                    val propertyName = m_indicator.getText(`var`.m_parameters[1])
                    if (propertyName.indexOf("cell.") != -1) {
                        var rowIndex = -1
                        if (m_indicator.getText(`var`.m_parameters[2]) == "selectedrow") {
                            if (grid.selectedRows.size > 0) {
                                rowIndex = grid.selectedRows[0].index
                            }
                        } else {
                            rowIndex = m_indicator.getValue(`var`.m_parameters[2]).toInt()
                        }
                        val colName = m_indicator.getText(`var`.m_parameters[3])
                        val row = grid.getRow(rowIndex)
                        if (row != null) {
                            val cell = row.getCell(colName)
                            cell.setAttribute(
                                propertyName.replace("cell.", ""),
                                m_indicator.getText(`var`.m_parameters[4])
                            )
                        }
                    }
                }
            }
        }
        return 0.0
    }

    /*
    * 开始秒表
    */
    private fun STARTTIMER(`var`: CVariable): Double {
        val control = m_xml!!.findView(m_indicator.getText(`var`.m_parameters[0]))
        control.startTimer(
            m_indicator.getValue(`var`.m_parameters[1]).toInt(),
            m_indicator.getValue(`var`.m_parameters[2]).toInt()
        )
        return 0.0
    }

    /*
    * 结束秒表
    */
    private fun STOPTIMER(`var`: CVariable): Double {
        val control = m_xml!!.findView(m_indicator.getText(`var`.m_parameters[0]))
        control.stopTimer(m_indicator.getValue(`var`.m_parameters[1]).toInt())
        return 0.0
    }

    /*
    * 更新布局
    */
    private fun UPDATE(`var`: CVariable): Double {
        if (m_xml != null) {
            val pLen = if (`var`.m_parameters != null) `var`.m_parameters.size else 0
            if (pLen == 0) {
                m_xml!!.native.update()
            } else {
                val control =
                    m_xml!!.findView(m_indicator.getText(`var`.m_parameters[0]))
                control?.update()
            }
        }
        return 0.0
    }

    companion object {
        /*
    * 方法
    */
        private const val FUNCTIONS =
            "GETATTRIBUTE,SETATTRIBUTE,GETSENDER,ALERT,INVALIDATE,STARTTIMER,STOPTIMER,GETTOUCHBUTTON,GETTOUCHPOINT,GETCLICKS,GETKEY,UPDATE,CALLFUNCTION,GETNEWTIMERID"

        /*
    * 前缀
    */
        private const val PREFIX = ""

        /*
    * 开始索引
    */
        private const val STARTINDEX = 2000

        /*
    * 创建指标
    */
        fun createIndicator(script: String?, xml: FCUIXml?): FCScript {
            val newScript = FCScript()
            val table = FCDataTable()
            newScript.dataSource = table
            val index = STARTINDEX
            val functions =
                FUNCTIONS.split("[,]").toTypedArray()
            val functionsSize = functions.size
            for (i in 0 until functionsSize) {
                newScript.addFunction(NFunctionEx(newScript, index + i, functions[i], xml))
            }
            newScript.script = script
            table.addColumn(0)
            table[0.0, 0] = 0.0
            newScript.onCalculate(0)
            return newScript
        }
    }

    /*
    * 创建方法
    */
    init {
        m_ID = id
        m_name = name
        m_xml = xml
    }
}